//
//  KKItemListController.m
//  DoMi
//
//  Created by 康凯 on 16/10/28.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKItemListController.h"
#import "UIColor+Hex.h"
#import "KKVideoDetailController.h"
#import "KKStudyHttp.h"
#import "KKVideoCell.h"

@interface KKItemListController ()<UITableViewDelegate,UITableViewDataSource>

@property(weak,nonatomic) UITableView *tableView;

@property(strong,nonatomic) NSArray *listDatas;


@end

@implementation KKItemListController

-(NSArray *)listDatas{
    if(!_listDatas){
        _listDatas=[NSArray array];
    }
    return _listDatas;
}
-(void)viewWillAppear:(BOOL)animated{
    [self setNavBlack];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithHexString:@"#EAEBEC"];
    self.navigationItem.title=self.barTitle;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(5, 5, screenWidth-10, screenHeight)];
    self.tableView=tableView;
    self.tableView.backgroundColor=[UIColor clearColor];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.showsVerticalScrollIndicator=NO;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    
    if([self isEmptyString:self.codeId]){
        [self getBestList];
    }else{
        [self getTopItemList];
    }
    
}

#pragma mark tableview datasource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listDatas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KKVideoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"KKVideoCell"];
    if(!cell){
        cell=[[[NSBundle mainBundle] loadNibNamed:@"KKVideoCell" owner:self options:nil] lastObject];
    }
    [cell initData:self.listDatas[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backItem;
    KKVideoDetailController *detail=[[KKVideoDetailController alloc]init];
    KKStudyList *list= self.listDatas[indexPath.row];
    detail.videoId=list.videoid;
    [self.navigationController pushViewController:detail animated:YES];
}

#pragma mark 接口--本月最佳列表
-(void)getBestList{
    [self showDialog];
    NSDictionary *params=@{@"uid":self.uid,@"token":self.token};
    [KKStudyHttp getBestList:params success:^(NSArray *datas) {
        self.listDatas=datas;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}

#pragma mark 接口--首页item列表
-(void)getTopItemList{
    [self showDialog];
    NSDictionary *params=@{@"uid":self.uid,@"token":self.token,@"code":self.codeId};
    [KKStudyHttp getTopItemList:params success:^(NSArray *datas) {
        self.listDatas=datas;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}



@end
